Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chat): Disable chat input when sending input #1781

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gadenbuie
Copy link
Collaborator

While reading through the code (and debugging something else I was working on), I noticed that we globally disable the input when setting the input value to an empty string.

This initially struck me as a bug – we'd want to disable the send input button but not the whole input – until I realized that it's part of the input/response lifecycle and we're expecting the input to be re-enabled when the next response happens.

This PR updates the code to explicitly set this.disabled = true in ChatInput.#sendInput() along with a note about the purpose and how it's unset.

While here, I also realized that if the user starts typing in the input while waiting for a response, but the user finishes before the response comes back, the input will be enabled but the send button won't. I added logic to recheck the button state when the input disabled attributes changes.

import asyncio

from shiny import App, ui

app_ui = ui.page_fillable(
    ui.panel_title("Hello Shiny Chat"),
    ui.chat_ui("chat"),
    fillable_mobile=True,
)

# Create a welcome message
welcome = ui.markdown(
    """
    Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
    simply repeat it back to you. For more examples, see this
    [folder of examples](https://github.com/posit-dev/py-shiny/tree/main/examples/chat).
    """
)


def server(input, output, session):
    chat = ui.Chat(id="chat", messages=[welcome])

    # Define a callback to run when the user submits a message
    @chat.on_user_submit
    async def _():
        # Get the user's input
        user = chat.user_input()
        # Append a response to the chat
        await asyncio.sleep(5)
        await chat.append_message(f"You said: {user}")


app = App(app_ui, server)

@gadenbuie gadenbuie requested a review from cpsievert November 21, 2024 14:38
@gadenbuie gadenbuie changed the title fix(chat): Disable chat input when sending input rather than setting the input value fix(chat): Disable chat input when sending input Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant